package com.yiwei.hdb.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.CreditScoreAdapter;
import com.yiwei.hdb.api.callback.PointLogContract;
import com.yiwei.hdb.api.requestInfo.PointLogRequest;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.model.PointLogModel;
import com.yiwei.hdb.util.OnLoadeMoreListener;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.view.CircularProgress;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/5/26.
 */
//信用积分
public class PointLogDetailActivity extends BaseActivity implements PointLogContract.View, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.list)
    RecyclerView list;
    CreditScoreAdapter adapter;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.nothing)
    TextView nothing;
    @BindView(R.id.progressBar)
    CircularProgress loading;
    private PointLogRequest api;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            api.getPointLog(page, 10);
        }
    };
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_point_log_detail);
        unbinder = ButterKnife.bind(this);
        baseInitView(this, "PointLogDetailActivity", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        setBackIntent();
        setTitleText("信用分明细");
        refresh.setOnRefreshListener(this);
        api = PointLogRequest.create();
        api.attachView(this);
        adapter = new CreditScoreAdapter(this);
        list.setAdapter(adapter);
        list.addOnScrollListener(onLoadeMoreListener);
        api.getPointLog(1, 10);
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null && refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getPointLogSuccess(int pageIndex, PointLogModel model) {
        if (pageIndex <= model.data.pager.getAllPages()) {
            adapter.add(model.data.lists, pageIndex == 1);
        } else {
            ToastUtil.makeText(getApplicationContext(), getResources().getString(R.string.no_more_date_text), Toast.LENGTH_SHORT);
        }
        if (adapter.getmSource().size() == 0 && model.data.lists.size() <= 0) {
            nothing.setVisibility(View.VISIBLE);
        } else {
            nothing.setVisibility(View.GONE);
        }
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (b) {
            if (loading != null) loading.setVisibility(View.VISIBLE);
        } else {
            if (loading != null) loading.setVisibility(View.GONE);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        onLoadeMoreListener.setPage(1, 0, false);
        api.getPointLog(1, 10);
    }
}
